<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.ArrayList,
                 com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.Functionality,
				 com.scalar.chiptrack.sales.ContactPerson"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>


<chiptrack:checkLogon sourcePage="/contactPersonAddressBook.do"/>

<html:html locale="true">

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<%
    String closeWindowAfterSave = ( String ) request.getAttribute( "closeWindowAfterSave" );
%>
<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel=stylesheet>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>

<script language='javascript'>

<% if ( closeWindowAfterSave != null && closeWindowAfterSave.equalsIgnoreCase( "true" ) ) { %>
    window.close();
<% } %>


function save()
{

    var form        = document.contactPersonAddressBookForm;
    var count       = form.contactPersonsCount.value;
    var sourceType  = form.sourceType.value;
    /*var showAgain   = form.showAgain.value;*/

    var emailid = '';
    for( var i = 0; i < count; i++ )
    {
        var checked = form['contactPersonsList[' + i + '].isChecked'].checked;

        if( count == 1 )
        {
            form.contactPersonSelected.value = "false"
        }
        else
        {
            form.contactPersonSelected[i].value = "false"    
        }

        if( checked == true)
        {

            var emailValue = form['contactPersonsList[' + i + '].email'].value;
            var contPerson = form['contactPersonsList[' + i + '].contactPerson'].value;
            if( count == 1 )
            {
                form.contactPersonSelected.value = "true"
            }
            else
            {
                form.contactPersonSelected[i].value = "true"    
            }
            if( emailid != '')
            {
                emailid =  emailid + " , " + emailValue;
            }
            else
            {
                emailid =  emailValue;
            }
        }
    }

    if( sourceType == 'Quotation' )
    {
        var toUserslength = window.opener.document.sendQuoteMailForm.toUsers.length ;

        if( toUserslength != null )
        {
            window.opener.document.sendQuoteMailForm.toUsers[0].value = emailid;
        }
        else
        {
            window.opener.document.sendQuoteMailForm.toUsers.value =  emailid;
        }
        window.opener.document.sendQuoteMailForm.showAgain.value = "true";    
    }
    else
    {
        var toUserslength = window.opener.document.sendAckMailForm.toUsers.length ;

        if( toUserslength != null )
        {
            window.opener.document.sendAckMailForm.toUsers[0].value = emailid;
        }
        else
        {
            window.opener.document.sendAckMailForm.toUsers.value =  emailid;
        }
        window.opener.document.sendAckMailForm.showAgain.value = "true";
    }
    document.contactPersonAddressBookForm.action = '<%=chipTracContext%>/contactPersonAddressBook.do?action=saveContactPersons&closeWindowAfterSave=true';
    form.submit();
}

function reset()
{
    document.contactPersonAddressBookForm.reset();
}

function closeWindow( form ) 
{
 	window.close();
}

</script>

</HEAD>
<BODY bgColor="#FDF5E6" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0" >

<html:form name="contactPersonAddressBookForm" type="com.scalar.chiptrack.sales.forms.ContactPersonAddressBookForm" action="/contactPersonAddressBook.do">
<table border="0" width="100%" align="center">
	<tr>
        <td bgcolor="#993333" align="center" class="NArialW"><font color='#FFFFFF'>Contact Person Details</font></td>
    </tr>
</table>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20>
<table border="0" width="90%" align="center">
	<tr>
		<td width="100%">
			<table border="0" width="100%" cellspacing='10' cellpadding='2' align='left'>
				<tr>
					<td align=right>
                        <a href="javascript:window.close();" title='Click here to close this window' onMouseOver="window.status='Click here to close this window';return true"><font class='NArialL'><b>[Close this window]</b></font></a>
                        <html:hidden name="contactPersonAddressBookForm" property="sourceType"/>
                    </td>
				</tr>
				<tr><td><chiptrack:errors styleClass='NArialL' /></td></tr>
				<tr><td><chiptrack:info styleClass='NArialL' /></td></tr>
				<tr><td align="left" class='NArialL'><FONT id="errors" styleClass="NArialL" color='FF0000'></font></td></tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
            <bean:size id="contactPersonListSize" name="contactPersonAddressBookForm" property="contactPersonsList"/>
            <table border="0" width="70%" align="center">
				<tr>
					<td width="5%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Select</font></font></td>
					<td width="15%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Name</font></font></td>
					<td width="15%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Email</font></font></td>
                    <td width="15%" align="center" bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Type</font></font></td>
				</tr>
                <logic:greaterThan name="contactPersonListSize" value="0">
                <nested:iterate id="contactPerson" name="contactPersonAddressBookForm" property="contactPersonsList" type="com.scalar.chiptrack.sales.ContactPerson" indexId="iteratorIdx">
				<tr <%if(iteratorIdx.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%> >
				    <td nowrap align="left">
                        <nested:checkbox property="isChecked" />
                        <input type='hidden' name='contactPersonSelected'>
                    </td>
                    <td nowrap align="left" class="NArialL">
                        <nested:write property="contactPerson" />
                        <nested:hidden property="contactPerson" />
                    </td>
				    <td nowrap align="left" class="NArialL">
                        <nested:write property="email" />
                        <nested:hidden property="email" />
                    </td>
                    <td nowrap align="left" class="NArialL">
                        <nested:write property="contactPersonType" />
                        <nested:hidden property="contactPersonType" />
                    </td>
				</tr>
				</nested:iterate>                
                <tr><td colspan="4"><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=50></td></tr>
				<tr>
                    <td colspan="4" align=center>
                        <input type='hidden' name='contactPersonsCount' value="<bean:write name='contactPersonListSize'/>"/>
                        <a href="javascript:save()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/submit.gif" width="53" height="17"></a>
				        <a href="javascript:reset()"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17"></a>
				    </td>
                </tr>
                </logic:greaterThan>
                <logic:equal name="contactPersonListSize" value="0">
                <tr>
                    <td colspan="3" align="center" bgcolor="#FFFFEE">
                        <font class="NArialL"><< Records not found >></font>
                    </td>
                </tr>
                </logic:equal>
            </table>						
		</td>
	</tr>
</table>
<%--<input type="hidden" name="showAgain" value="<%=request.getAttribute("showAgain")%>">--%>
</html:form>

</body>

</html:html>